#include "trans_handle_tables_act.h"

//--数据库操作-------------------------------------------------
//todo上传文件插入数据库表操作的函数
//查数据库表，文件夹中是否有对应的文件
int CheckFileExist(user_session_t *session, char *fileName, char *md5)
{
    char query[1024];
    snprintf(query, sizeof(query), "select filename,md5 from files where parent_id = '%d' and username = '%s'", session->pStack->pTop->file_id,session->username); // 父亲id
    printf("file_id = %d\n", session->pStack->pTop->file_id);
    if (mysql_query(session->conn, query) != 0)
    {
        fprintf(stderr, "Query failed: %s\n", mysql_error(session->conn));
        return -1;
    }
    MYSQL_RES *result = mysql_store_result(session->conn);
    if (result == NULL)
    {
        fprintf(stderr, "mysql_store_result() failed: %s\n", mysql_error(session->conn));
        return -1;
    }
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result)) != NULL)
    {
        if (strcmp(fileName, row[0]) == 0) // 有同名文件
        {
            memcpy(md5, row[1], 32);
            md5[32] = '\0';
            return 1;
        }
    }
    bzero(md5,33);
    return 0; // 没有同名文件
}
//对数据库进行插入操作，新增了文件夹下的文件
int MYSQL_Data_Insert(user_session_t *session, char *filename, char *md5_value_insert)
{
    int len = strlen(session->path);
    if (session->path[len - 1] != '/')
    {
        strcat(session->path, "/");
    }
    strcat(session->path, filename);
    char query[4096];
    snprintf(query, sizeof(query), "insert into files (parent_id,filename,user_id,username,md5,type,path) value ('%d','%s','%d','%s','%s','f','%s')",
             session->pStack->pTop->file_id,
             filename,
             session->user_id,
             session->username,
             md5_value_insert,
             session->path);
    int qres = mysql_query(session->conn, query);
    if (qres != 0)
    {
        fprintf(stderr, "Query failed: %s\n", mysql_error(session->conn));
        return -1;
    }
    return 1;
}
//对数据库进行数据更新操作，更改了文件夹下的文件名
int MYSQL_Data_Replace(user_session_t *session, char *filename, char *md5_value_replace)
{
    char query[1024];
    snprintf(query, sizeof(query), "update files set md5 = '%s' where filename = '%s' AND parent_id = '%d'", 
             md5_value_replace,filename,session->pStack->pTop->file_id);
    int qres = mysql_query(session->conn, query);
    if (qres != 0)
    {
        fprintf(stderr, "Query failed: %s\n", mysql_error(session->conn));
        return -1;
    }
    return 1;
}
//--哈希-------------------------------------------------
//主调函数需要传入一个文件名，字符数组,将计算出的哈希值返回给主调函数
int Get_File_Md5Value(char *filename,char *str_ptr){

    char md5_str[MD5_STR_LEN + 1];
    int ret = Compute_file_md5(filename, md5_str);
    if (0 == ret){
         printf("MD5 value calculation successful\n");
     }
    //数组一共可以装33个字节
    strncpy(str_ptr, md5_str,33);
    //str_ptr[32] = '\0';
    return 0;
}
//计算文件哈希值的函数
int Compute_file_md5(const char *file_path, char *md5_str)
{
	int i;
	int fd;
	int ret;
	unsigned char data[READ_DATA_SIZE];
	unsigned char md5_value[MD5_SIZE];
	MD5_CTX md5;
	fd = open(file_path, O_RDONLY);
	if (-1 == fd)
	{
		perror("open");
		return -1;
	}
	// init md5
	MD5Init(&md5);
	while (1)
	{
		ret = read(fd, data, READ_DATA_SIZE);
		if (-1 == ret)
		{
			perror("read");
			return -1;
		}
		MD5Update(&md5, data, ret);
		if (0 == ret || ret < READ_DATA_SIZE)
		{
			break;
		}
	}
	close(fd);
	MD5Final(&md5, md5_value);
	for(i = 0; i < MD5_SIZE; i++)
	{
		snprintf(md5_str + i*2, 2+1, "%02x", md5_value[i]);
	}
	md5_str[MD5_STR_LEN] = '\0'; // add end
	return 0;
}

//---------------------------------------------------
